package com.neusoft.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neusoft.entity.Pay;
import com.neusoft.entity.PayMoney;
import com.neusoft.entity.Rent;
import com.neusoft.entity.common.CommonResult;
import com.neusoft.service.PayMoneyService;
import com.neusoft.service.PayService;
import com.neusoft.service.RentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * (Pay)表控制层
 *
 * @author makejava
 * @since 2020-06-16 11:29:12
 */
@Controller
@RequestMapping("pay")
public class PayController {
    /**
     * 服务对象
     */
    @Resource
    @Autowired
    private PayService payService;

    @Autowired
    private PayMoneyService payMoneyService;

    @GetMapping("queryById/{id}")
    @ResponseBody
    public Object queryById(@PathVariable Integer id){
        System.out.println("查询单条数据"+id);
        Pay bean = payService.queryById(id);
        System.out.println("bean对象为："+bean);
        return bean;
    }

    @PostMapping("deleteById/{ids}")
    @ResponseBody
    public boolean deleteById(@PathVariable Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return false;
        }
        System.out.println("批量删除数据");
        System.out.println(ids[0]);
        payService.deleteById(Arrays.asList(ids));
        return true;
    }

    @PostMapping("deleteById1/{id}")
    @ResponseBody
    public boolean deleteById1(@PathVariable Integer id) {
        System.out.println("删除单条数据");
        if ( id == null) {
            return false;
        }
        System.out.println(id);
        return payService.deleteById1(id);
    }

    @PostMapping("queryAll/{page}/{limit}")
    @ResponseBody
    public Object queryAll(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody Pay bean) {
        System.out.println(page);
        System.out.println(limit);
        System.out.println(bean);
        CommonResult<Pay> result = new CommonResult<>();
        IPage<Pay> ipage = payService.queryAllByLimit(page, limit, bean);
        result.setCode(0);
        result.setCount(ipage.getTotal());
        result.setData(ipage.getRecords());
        System.out.println("数据查询成功");
        System.out.println(result);
        return result;
    }

    @PutMapping("save")
    @ResponseBody
    public Object save(@RequestBody Pay bean) {
        System.out.println("修改和添加数据");
        boolean result;
        //判断账号是否存在
        //判断是添加还是修改
        if (bean.getId() != null) {
            result = payService.update(bean) > 0;
        } else {
            result = payService.insert(bean).getId() != null;
        }
        return result;
    }

    @PutMapping("payMoney")
    @ResponseBody
    public Object pay(@RequestBody PayMoney bean) {
        System.out.println("确认缴纳租金bean对象为："+bean);
        Pay pay = payService.queryById(bean.getPayid());
        boolean result;
        pay.setTotal(pay.getTotal()-bean.getMoney());
        if(pay.getTotal() == 0 || pay.getTotal() < 0) {
            pay.setTotal(0);
            pay.setState("否");
        }
        payMoneyService.deleteById1(bean.getPayid());
        result = payService.update(pay) > 0;
        return true;
    }

    @GetMapping("queryByPayId/{payId}")
    @ResponseBody
    public Object queryByPayId(@PathVariable Integer payId){
        return payMoneyService.queryByPayId(payId);
    }

    @PostMapping("insert")
    @ResponseBody
    public boolean insert(@RequestBody PayMoney bean) {
        System.out.println("插入payMoney对象为："+bean);
        return payMoneyService.insert(bean) != null;
    }

}